﻿
Public Class frmMain
    Dim ItemArray() As Item
    Dim Player As New Character

    Dim iItemArrayIndex As Integer = 0

    Private _IsDebug As Boolean = False '-- turn on if you want to see information in the debug window about the object's state.

    Private WithEvents _timer As Timer = Nothing '-- the 'game engine'.
    Private _oUser As Character '-- our user class
    Private _oEnemy As Enemy = Nothing '-- An enemy class that follows a way point that is hard coded.

    Private Sub btnNewCharacter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewCharacter.Click
        Dim NameDialog As New Input
        Dim sCharacterName As String

        NameDialog.Visible = False
        NameDialog.Text = "Name Your Character"
        NameDialog.lblPrompt.Text = "Enter Your Characters Name:"
        If NameDialog.ShowDialog() = DialogResult.OK Then
            sCharacterName = NameDialog.txtInput.Text
            Player.setCharacterAttributes(sCharacterName)
        End If

        btnNewCharacter.Enabled = False
        Player.showStatusInfo()
        CharacterInfo.Show()
    End Sub

    Private Sub btnShowCharacter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowStatus.Click
        Player.showStatusInfo()
        CharacterInfo.Show()
    End Sub

    Private Sub btnAddLevel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLevel.Click
        Player.addLevel()
        Player.showStatusInfo()
    End Sub

    Private Sub btnRandomItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandomItem.Click
        iItemArrayIndex = iItemArrayIndex + 1

        Dim RndItem As Item = New Item(iItemArrayIndex)
        Player.Inventory.Add(RndItem)


        lblItemArrayTotal.Text = Player.Inventory.Count
        lblItemArrayNumber.Text = RndItem.ItemID
        lblItemBaseType.Text = RndItem.ItemBaseType
        lblItemExtendedType.Text = RndItem.ItemExtendedType
    End Sub

    Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
    End Sub

    Private Sub btnCreate100Items_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate100Items.Click
        For i = 1 To 100
            iItemArrayIndex = iItemArrayIndex + 1

            Dim RndItem As Item = New Item(iItemArrayIndex)
            Player.Inventory.Add(RndItem)


            lblItemArrayTotal.Text = Player.Inventory.Count
            lblItemArrayNumber.Text = RndItem.ItemID
            lblItemBaseType.Text = RndItem.ItemBaseType
            lblItemExtendedType.Text = RndItem.ItemExtendedType
        Next
    End Sub

    Public Sub btnDisplayRandomItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRandomItem.Click
        Dim RndItemIndex As Integer

        RndItemIndex = Roll(1, iItemArrayIndex)

        lblItemArrayTotal.Text = Player.Inventory.Count
        lblItemArrayNumber.Text = Player.Inventory(RndItemIndex).ItemID
        lblItemBaseType.Text = Player.Inventory(RndItemIndex).ItemBaseType
        lblItemExtendedType.Text = Player.Inventory(RndItemIndex).ItemExtendedType

    End Sub


    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        '-- Setup the display to not flicker and procss the graphics smoothly.
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

        Me.Height = 500
        Me.Width = 500

        '-- create our user to be drawn.
        _oUser = New Character
        _oUser.ScreenHeight = Me.Height '-- to prevent moving off the screen!
        _oUser.ScreenWidth = Me.Width '-- to prevent moving off the screen!
        If _IsDebug Then Debug.WriteLine(_oUser.Print)

        _oEnemy = New Enemy
        _oEnemy.ScreenHeight = Me.Height '-- to prevent moving off the screen!
        _oEnemy.ScreenWidth = Me.Width '-- to prevent moving off the screen!
        If _IsDebug Then Debug.WriteLine(_oEnemy.Print)

        '-- setup our timer as the engine to process.
        _timer = New Timer
        _timer.Interval = 100
        _timer.Start()
    End Sub

    '-- the "game engine".
    Private Sub _timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _timer.Tick
        '-- When the engine ticks proces the input into something our user class can do.
        _oUser.ProcessState()
        If _IsDebug Then Debug.WriteLine(_oUser.Print)

        _oEnemy.ProcessState()
        If _IsDebug Then Debug.WriteLine(_oEnemy.Print)

        '-- Redraw the scene
        Me.Refresh()
    End Sub

    '-- The take the form's input and set that action to our user object's next action on deck.
    Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        '-- To avoid the issue of having a stack of key presses if the user holds the key down only accept one 
        '-- key stroke until it is processed and the graphics updated.
        If _oUser.NextAction = Entity.MovementDirection.NONE Then
            Select Case e.KeyCode
                Case Keys.Up, Keys.W
                    _oUser.NextAction = Entity.MovementDirection.UP
                Case Keys.Down, Keys.S
                    _oUser.NextAction = Entity.MovementDirection.DOWN
                Case Keys.Right, Keys.D
                    _oUser.NextAction = Entity.MovementDirection.RIGHT
                Case Keys.Left, Keys.A
                    _oUser.NextAction = Entity.MovementDirection.LEFT
            End Select
        End If
    End Sub

    Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        _oEnemy.Paint(e.Graphics)

        '-- Pain the changes to the screen.
        _oUser.Paint(e.Graphics)
    End Sub
End Class
